package br.com.orion.sga.controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;

import br.com.orion.sga.exception.DaoException;
import br.com.orion.sga.exception.FileGenerateException;
import br.com.orion.sga.model.entities.Aluno;
import br.com.orion.sga.service.AlunoService;
import br.com.orion.sga.service.VencimentoService;
import br.com.orion.sga.utils.GenerateCarne;
import br.com.orion.sga.utils.MessageHandler;

@ManagedBean(name = "studentManagerController")
@ViewScoped
public class StudentManagerController {

	/*
	 * Services
	 */
	@ManagedProperty("#{alunoService}")
	private AlunoService alunoService;
	
	@ManagedProperty("#{vencimentoService}")
	private VencimentoService vencimentoService;
	
	/*
	 * List of students persisted on database
	 */
	private List<Aluno> alunos;
	
	/*
	 * List of Students filtered in data table
	 */
	private List<Aluno> filteredAlunos;
	
	
	@PostConstruct
	public void init() {
		listStudents();
	}
	
	/**
	 * List all students
	 */
	public void listStudents() {
		alunos = alunoService.listAll();
		System.out.println(System.getProperty("catalina.base"));
	}
	
	
	public void deleteStudent(Aluno student) {
		
		try {
			alunoService.delete(student);
			alunos.remove(student);
			MessageHandler.placeInfoMessage("Aluno removido com sucesso!");
		} catch (DaoException e) {
			MessageHandler.placeErrorMessage(e.getMessage());
		}
	}
	
	
	public void generateCarne(Aluno student) {
		GenerateCarne carne = new GenerateCarne(student);
		try {
			carne.generateCarne();
		} catch (FileGenerateException e) {
			MessageHandler.placeErrorMessage(e.getMessage());
		}
	}
	
	/** 
	 * GETTERS AND SETTERS
	 */
	public AlunoService getAlunoService() {
		return alunoService;
	}
	
	public void setAlunoService(AlunoService alunoService) {
		this.alunoService = alunoService;
	}
	
	public VencimentoService getVencimentoService() {
		return vencimentoService;
	}
	
	public void setVencimentoService(VencimentoService vencimentoService) {
		this.vencimentoService = vencimentoService;
	}
	
	public List<Aluno> getAlunos() {
		return alunos;
	}

	public void setAlunos(List<Aluno> alunos) {
		this.alunos = alunos;
	}
	
	public List<Aluno> getFilteredAlunos() {
		return filteredAlunos;
	}
	
	public void setFilteredAlunos(List<Aluno> filteredAlunos) {
		this.filteredAlunos = filteredAlunos;
	}
}
